package com.sprucetec.common.config;

import org.apache.commons.lang.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther: wangshaobo
 * @Date: 2018/9/20 11:07
 * @Description: redisson 属性配置类
 */


@Configuration
@AutoConfigureAfter(value = {RedissonProperties.class})
public class RedissonConfiguration {

    @Autowired
    private RedissonProperties redssionProperties;


    @Bean
    @ConditionalOnProperty(name="redisson.master-name")
    RedissonClient getRedisson() {
        Config config = new Config();
        SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
                .setMasterName(redssionProperties.getMasterName())
                .setTimeout(redssionProperties.getTimeout())
                .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
                .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());

        if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
            serverConfig.setPassword(redssionProperties.getPassword());
        }

        RedissonClient redisson = Redisson.create(config);
        System.out.println("config info ======="+redisson.getConfig().toString());
        return redisson;
    }




}
